package top.cardone.context.func;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import top.cardone.context.ApplicationContextHolder;
import top.cardone.core.util.func.Func1;

/**
 * Created by Administrator on 2016/9/14.
 */
public class ConversionFunc implements Func1<Object, Object> {
	@Setter
	private String converterBenaId;

	@Setter
	@Getter(lazy = true)
	private final Converter converter = this.converter();

	private Converter converter() {
		return ApplicationContextHolder.getBean(Converter.class, converterBenaId);
	}

	@Override
	public Object func(Object obj) {
		if (obj == null) {
			return null;
		}

		if (obj instanceof String) {
			if (StringUtils.isBlank((String) obj)) {
				return null;
			}
		}

		return this.getConverter().convert(obj);
	}
}